package com.my.chapter14;

import com.sun.xml.internal.ws.wsdl.writer.document.Part;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class practice13 {
    public static void main(String[] args) {
        TypeCounter counter = new TypeCounter(Parts.class);
        Parts part;
        for(int i = 0; i < 20; i++) {
            part = Parts.createRandom();
            System.out.print(part.getClass().getSimpleName() + " ");
            counter.count(part);
        }
        System.out.println("");
        System.out.println(counter);
    }
}

interface Factory<T>{
    T create();
}

class Parts{
    @Override
    public String toString() {
        return getClass().getSimpleName();
    }

    static List<Factory<? extends Parts>> partFactories = new ArrayList<Factory<? extends Parts>>();
    static {
        partFactories.add(new FuelFilter.Factory());
        partFactories.add(new AirFilter.Factory());
        partFactories.add(new CabinAirFilter.Factory());
        partFactories.add(new OilFilter.Factory());
        partFactories.add(new FanBelt.Factory());
        partFactories.add(new PowerSteeringBelt.Factory());
        partFactories.add(new GeneratorBelt.Factory());
    }
    private static Random rand = new Random(47);
    public static Parts createRandom(){
        int n = rand.nextInt(partFactories.size());
        return partFactories.get(n).create();
    }
}

class Filter extends Parts{}

class FuelFilter extends Filter{
    public static class Factory implements com.my.chapter14.Factory<FuelFilter>{
        @Override
        public FuelFilter create() {
            return new FuelFilter();
        }
    }
}

class AirFilter extends Filter{
    public static class Factory implements com.my.chapter14.Factory<AirFilter>{
        @Override
        public AirFilter create() {
            return new AirFilter();
        }
    }
}

class CabinAirFilter extends Filter{
    public static class Factory implements com.my.chapter14.Factory<CabinAirFilter>{
        @Override
        public CabinAirFilter create() {
            return new CabinAirFilter();
        }
    }
}

class OilFilter extends Filter{
    public static class Factory implements com.my.chapter14.Factory<OilFilter>{

        @Override
        public OilFilter create() {
            return new OilFilter();
        }
    }
}

class Belt extends Parts{}

class FanBelt extends Belt{
    public static class Factory implements com.my.chapter14.Factory<FanBelt>{

        @Override
        public FanBelt create() {
            return new FanBelt();
        }
    }
}

class GeneratorBelt extends Belt{
    public static class Factory implements com.my.chapter14.Factory<GeneratorBelt>{

        @Override
        public GeneratorBelt create() {
            return new GeneratorBelt();
        }
    }
}

class PowerSteeringBelt extends Belt{
    public static class Factory implements com.my.chapter14.Factory<PowerSteeringBelt>{

        @Override
        public PowerSteeringBelt create() {
            return new PowerSteeringBelt();
        }
    }
}



